package com.cskaoyan.service;

import com.cskaoyan.bean.Ad;
import com.cskaoyan.bean.AdExample;
import com.cskaoyan.mapper.AdMapper;
import com.cskaoyan.vo.AdVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ description:
 * @ author:台
 **/
@Service
public class AdServiceImpl implements AdService {

    @Autowired
    AdMapper adMapper;

    @Override
    public AdVo adList(Integer page, Integer limit, String sort, String order) {
        PageHelper.startPage(page,limit);

        AdExample adExample = new AdExample();
        adExample.setOrderByClause(sort + "" + order);
        adExample.createCriteria().andDeletedEqualTo(false);
        List<Ad> adList = adMapper.selectByExample(adExample);

        PageInfo<Ad> adPageInfo = new PageInfo<>(adList);
        long total = adPageInfo.getTotal();

        AdVo adVo = new AdVo();
        adVo.setItems(adList);
        adVo.setTotal(total);
        return adVo;
    }

    @Override
    public void adDelete(Ad ad) {
        AdExample adExample = new AdExample();
        AdExample.Criteria criteria = adExample.createCriteria();
        criteria.andIdEqualTo(ad.getId());
        Ad ad1 = new Ad();
        ad1.setDeleted(true);
        adMapper.updateByExampleSelective(ad1,adExample);
    }

    @Override
    public Ad adCreate(Ad ad) {
        Date date = new Date();
        ad.setAddTime(date);
        ad.setEndTime(date);
        adMapper.insertSelective(ad);
        Integer id = ad.getId();
        Ad data = adMapper.selectByPrimaryKey(id);
        return data;
    }

    @Override
    public Ad adUpdate(Ad ad) {
        Date date = new Date();
        ad.setAddTime(date);
        ad.setEndTime(date);
        adMapper.updateByPrimaryKeySelective(ad);
        Ad data = adMapper.selectByPrimaryKey(ad.getId());
        return data;
    }
}
